package com.nsu.cl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nsu.cl.pojo.Teacher;
import com.nsu.cl.service.TeacherService;
import com.nsu.cl.util.MD5;
import com.nsu.cl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "教师控制器")
@RestController
@RequestMapping("/teacherController")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    // get teacherController/getTeachers/1/3?name=&clazzName=  响应Result data=分页

    @ApiOperation("通过老师ID获取单个老师信息")
    @GetMapping("/getTeacherById/{id}")
    public Result getTeacherById(@ApiParam("老师ID") @PathVariable("id") Long id) {
        Teacher teacher = teacherService.getTeacherById(id);
        return Result.ok(teacher);
    }

    @ApiOperation("通过年级信息获取单个老师信息")
    @GetMapping("/getTeacherByGradeName")
    public Result getTeacherByGradeName(@ApiParam("老师部分信息") Teacher teacher) {
        List<Teacher> teacherList = teacherService.getTeacherByGradeName(teacher);
        return Result.ok(teacherList);
    }

    @ApiOperation("分页获取教师信息，带搜索条件")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(@ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
                              @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
                              Teacher teacher) {
        Page<Teacher> pageParam = new Page<>(pageNo, pageSize);

        IPage<Teacher> page = teacherService.getTeachersByOpr(pageParam, teacher);

        return Result.ok(page);
    }

    // post teacherController/saveOrUpdateTeacher 数据 Teacher  响应Result data=null

    @ApiOperation("添加或修改教师信息")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@ApiParam("要保存或修改的JSON格式的Teacher") @RequestBody Teacher teacher) {

        // 如果新增要对密码进行加密
        if (teacher.getId() == null || teacher.getId() == 0) {
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }

        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    // DELETE teacherController/deleteTeacher 请求的数据 JSON 数组[1,2,3] 响应Result date=null OK

    @ApiOperation("删除单个或多个教师信息")
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(@ApiParam("要删除的教师信息的id的JSON集合") @RequestBody List<Integer> ids) {
        teacherService.removeByIds(ids);
        return Result.ok();
    }
}
